package com.animee.tally.chart_frag;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.animee.tally.adapter.ChartAdapter;
import com.animee.tally.R;
import com.animee.tally.db.ChartItemBean;
import com.animee.tally.db.DBManager;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import java.util.ArrayList;
import java.util.List;

abstract public class BaseChartFragment extends Fragment {
    ListView chartLv;
    BarChart barChart;  // 柱状图的view
    TextView chartTv;   //提示是否显示柱状图
    int year=2020;
    int month = 10;
    List<ChartItemBean> mDatas;
    private ChartAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_chart_income, container, false);
        chartLv = view.findViewById(R.id.frag_chart_lv);
        //获取fragment所在的activity传入的数据
        Bundle bundle = getArguments();
        year = bundle.getInt("year");
        month = bundle.getInt("month");
        mDatas = new ArrayList<>();
        //设置适配器，添加头布局
        adapter = new ChartAdapter(getContext(), mDatas);
        chartLv.setAdapter(adapter);
        // 添加ListView的头布局
        addLVHeaderView();
        return view;
    }
    /* 给ListView添加头布局*/
    private void addLVHeaderView() {
        // 将布局转换成View对象
        View headerView = getLayoutInflater().inflate(R.layout.item_chartfrag_top,null);
        // 将view添加到ListView当中
        chartLv.addHeaderView(headerView);
        barChart = headerView.findViewById(R.id.item_chartfrag_chart);
        chartTv = headerView.findViewById(R.id.item_chartfrag_top_tv);
        barChart.getDescription().setEnabled(false); // 不显示描述
        barChart.setExtraOffsets(20, 20, 20, 20); // 设置饼图的偏移量，类似于内边距 ，设置视图窗口大小
        //设置坐标轴
        setAxis(year,month);
        // 设置坐标轴显示的数据
        setAxisData(year,month);
    }
    /* 设置坐标轴显示的数据*/
    abstract public void setAxisData(int year, int month) ;

    /*设置柱状图坐标轴的显示 方法必须重写*/
    public  void setAxis(int year, final int month) {
        // 设置x轴
        final XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);  // 设置x轴显示在下方，默认在上方
        xAxis.setDrawGridLines(true); // 将此设置为true，绘制该轴的网格线。
        xAxis.setLabelCount(31);  // 设置x轴上的标签个数
        xAxis.setTextSize(12f); // x轴上标签的大小

        // 设置x轴显示的值的格式
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int val = (int)value;
                if (val==0){
                    return  month+"-1";
                }
                if (val==14){
                    return month+"-15";
                }
                if (month==2){
                    if (val==27)
                        return month+"-28";
                }else if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
                    if (val==30)
                        return month+"-31";
                }else if (month==4||month==6||month==9||month==11){
                    if(val==29)
                        return month+"-30";
                }

                return "";
            }
        });
        xAxis.setYOffset(10); // 设置标签对x轴的偏移量，垂直方向

        //y轴在子类进行设置

    }

    public void setDate(int year,int month){
        this.year = year;
        this.month = month;
        barChart.clear();  //清空数据
        barChart.invalidate();  //重新绘制图标
        setAxis(year,month);
        setAxisData(year,month);
    }

    public void loadData(int year, int month, int kind) {
        List<ChartItemBean> list = DBManager.getChartListFromAccount(year, month, kind);
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
    }

}
